package com.app.utils;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;

import java.io.File;
import java.util.HashMap;

/**
 * 支持普通上传，普通下载和断点上传
 */
public class OssUtil {

    private Context context;

    public static final String bucket = "yuepa";

    public static final String endpoint = "oss-cn-shenzhen.aliyuncs.com";

    private OSS oss;
    private String callbackAddress;
    //根据实际需求改变分片大小
    private final static int partSize = 256 * 1024;

    private static OssUtil instance;
    private boolean isInited;//是否已经初始化了OSSClient

    private OssUtil() {

    }

    public static OssUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (OssUtil.class) {
                instance = new OssUtil();
                instance.context = context;
                instance.init();
            }
        }
        return instance;
    }

    private void init() {
        String accessKeyId = "LTAIgowPngvTJ6WF";
        String accessKeySecret = "0lyL4F2DNLl6Q4OhQBAgI3OOZq8Frv";
        String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
        // 明文设置secret的方式建议只在测试时使用，更多鉴权模式请参考后面的`访问控制`章节
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
//        // 明文设置secret的方式建议只在测试时使用，更多鉴权模式请参考后面的`访问控制`章节
//        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider("<accessKeyId>", "<accessKeySecret>");
        oss = new OSSClient(context.getApplicationContext(), endpoint, credentialProvider);

    }

    public void uploadImage(String name , String localFile, OSSCompletedCallback completedCallback, OSSProgressCallback progressCallback) {
        if (name.equals("")) {
            Log.w("AsyncPutImage", "ObjectNull");
            return;
        }

        File file = new File(localFile);
        if (!file.exists()) {
            Log.w("AsyncPutImage", "FileNotExist");
            Log.w("LocalFile", localFile);
            return;
        }


        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(bucket, name, localFile);

        if (callbackAddress != null) {
            // 传入对应的上传回调参数，这里默认使用OSS提供的公共测试回调服务器地址
            put.setCallbackParam(new HashMap<String, String>() {
                {
                    put("callbackUrl", callbackAddress);
                    //callbackBody可以自定义传入的信息
                    put("callbackBody", "filename=${name}");
                }
            });
        }
        if (progressCallback != null) {
            put.setProgressCallback(progressCallback);
        }

        OSSAsyncTask task = oss.asyncPutObject(put, completedCallback);
    }

}
